package com.example.cloud.controller.data;

import com.example.cloud.util.Result;
import com.example.cloud.util.DataGenerator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "数据生成", description = "测试数据生成相关接口")
@RestController
@RequestMapping("/api/data")
@RequiredArgsConstructor
public class DataGeneratorController {
    
    private final DataGenerator dataGenerator;
    
    /**
     * 生成测试数据
     */
    @Operation(summary = "生成测试数据", description = "批量生成指定数量的用户测试数据")
    @GetMapping("/generate")
    public Result<String> generateData(
            @Parameter(description = "要生成的数据条数", example = "10000")
            @RequestParam(value = "count", defaultValue = "10000") int count,
            @Parameter(description = "生成方法：batch-普通批量，named-命名参数", example = "batch")
            @RequestParam(value = "method", defaultValue = "batch") String method) {
        
        try {
            long startTime = System.currentTimeMillis();
            
            if ("named".equalsIgnoreCase(method)) {
                // 使用命名参数方式
                dataGenerator.generateUsersWithNamedParams(count);
            } else {
                // 使用普通批量方式
                dataGenerator.generateUsers(count);
            }
            
            long endTime = System.currentTimeMillis();
            return Result.success(String.format("成功生成 %d 条测试数据，耗时 %d 毫秒", 
                    count, (endTime - startTime)));
        } catch (Exception e) {
            return Result.error("生成数据失败：" + e.getMessage());
        }
    }
} 